package controller

import (
	"mini-app/internal/app/admin/service"
	"strconv"

	"192.168.1.75/go-pkg/response"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type GoodsReview struct{}

// List 评测列表
func (GoodsReview) List(c *gin.Context) {
	goodsType, _ := strconv.Atoi(c.Query("goods_type"))
	goodsName := c.Query("goods_name")
	visible, _ := strconv.Atoi(c.Query("visible"))
	auditStatus, _ := strconv.Atoi(c.Query("audit_status"))
	startTime, _ := strconv.Atoi(c.Query("start_time"))
	endTime, _ := strconv.Atoi(c.Query("end_time"))
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	list, total, err := service.GoodsReview{}.List(
		c.Request.Context(),
		goodsType, goodsName, visible, auditStatus,
		startTime, endTime,
		page, limit,
	)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, err)
}

// Audit 评测审核
func (GoodsReview) Audit(c *gin.Context) {
	var params struct {
		ID     string `json:"id"`
		Status int    `json:"status"`
		Reason string `json:"reason"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	err := service.GoodsReview{}.Audit(
		c.Request.Context(),
		params.ID,
		params.Status,
		params.Reason,
		c.GetString("user_id"),
	)
	response.JSON(c, nil, err)
}

// SetVisible 设置评测是否对外展示
func (GoodsReview) SetVisible(c *gin.Context) {
	var params struct {
		ReviewID string `json:"review_id"`
		Visible  int    `json:"visible"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	err := service.GoodsReview{}.SetVisible(c.Request.Context(), params.ReviewID, params.Visible)
	response.JSON(c, nil, err)
}

// AuditLog 审核日志
func (GoodsReview) AuditLog(c *gin.Context) {
	goodsType, _ := strconv.Atoi(c.Query("goods_type"))
	goodsName := c.Query("goods_name")
	auditStatus, _ := strconv.Atoi(c.Query("audit_status"))
	startTime, _ := strconv.Atoi(c.Query("start_time"))
	endTime, _ := strconv.Atoi(c.Query("end_time"))
	page, _ := strconv.Atoi(c.Query("page"))
	limit, _ := strconv.Atoi(c.Query("limit"))
	list, total, err := service.GoodsReview{}.AuditLog(
		c.Request.Context(),
		goodsType,
		goodsName,
		auditStatus,
		startTime, endTime,
		page, limit,
	)
	response.JSON(c, gin.H{
		"list":  list,
		"total": total,
	}, err)
}

// Update 更新评测
func (GoodsReview) Update(c *gin.Context) {
	var params struct {
		ID      string `json:"id"`
		Star    string `json:"star"`
		Content string `json:"content"`
		Images  string `json:"images"`
	}
	c.ShouldBindBodyWith(&params, binding.JSON)
	err := service.GoodsReview{}.Update(
		c.Request.Context(),
		c.GetString("openid"),
		params.ID,
		params.Star,
		params.Content,
		params.Images,
	)
	response.JSON(c, nil, err)
}
